import type { DramaAddRequest } from '@/api/drama-management';
import type { FormInstance, FormRules } from 'element-plus';
import type { Ref } from 'vue';
/**
 *
 * 注册验证监听
 *
 */
export function userValidateWatchUtil(elFormRef: Ref<FormInstance | null>, formData: Ref<DramaAddRequest>) {
  watch(
    () => formData.value.performerRoleList,
    () => {
      elFormRef.value?.validateField('performerRoleList');
    }
  );

  watch(
    () => formData.value.dramaCoverDyImage,
    () => {
      elFormRef.value?.validateField('dramaCoverDyImage');
    }
  );

  watch(
    () => formData.value.dramaCoverThumb,
    () => {
      elFormRef.value?.validateField('dramaCoverThumb');
    }
  );
  watch(
    () => formData.value.dramaCover,
    () => {
      elFormRef.value?.validateField('dramaCover');
    }
  );
  watch(
    () => formData.value.costDistributionUri,
    () => {
      elFormRef.value?.validateField('costDistributionUri');
    }
  );
}

/**
 * 裁剪框工具
 */
export function userUlesUtil(formData: Ref<DramaAddRequest>) {
  const dramaTitleMax = 20;
  const productionCostMax = 29;
  const avgDurationMax = 1000;
  const dramaDespMin = 20;
  const dramaDespMax = 200;
  const dramaRecommendationMax = 12;
  const allEpisodePriceMax = 9999;
  const priceMax = 9999;
  const seqNumMax = 9999;

  const rules = computed<FormRules<DramaAddRequest>>(() => {
    const _rules: FormRules<DramaAddRequest> = {
      dramaTitle: [
        { required: true, message: '请输入短剧名称', trigger: ['blur', 'change'] },
        { min: 1, max: dramaTitleMax, message: `长度在 1 到 ${dramaTitleMax} 个字符`, trigger: ['blur', 'change'] },
      ],
      organizationName: [{ required: true, message: '请输入制作机构', trigger: ['blur', 'change'] }],
      productionCost: [
        { required: true, message: '请输入制作成本', trigger: ['blur', 'change'] },
        { min: 0.1, max: productionCostMax, type: 'number', message: `制作成本在 1 到 ${productionCostMax} 万元`, trigger: ['blur', 'change'] },
      ],

      avgDuration: [
        { required: true, type: 'number', message: '请输入平均时长', trigger: ['blur', 'change'] },
        { min: 1, max: avgDurationMax, type: 'number', message: `时长在 1 到 ${avgDurationMax} 秒`, trigger: ['blur', 'change'] },
      ],
      isFree: [
        { required: true, type: 'number', message: '请选择是否免费', trigger: ['blur', 'change'] },
        { min: 0, max: 1, type: 'number', message: `是否免费不合法`, trigger: ['blur', 'change'] },
      ],
      categoryList: [{ required: true, type: 'array', message: '请选择视频分类', trigger: ['blur', 'change'] }],
      dramaCover: [{ required: true, type: 'string', message: '请上传海报', trigger: ['blur', 'change'] }],
      dramaCoverDyImage: [{ required: true, type: 'string', message: '请上传封面', trigger: ['blur', 'change'] }],
      dramaCoverThumb: [{ required: true, type: 'string', message: '请上传缩略图', trigger: ['blur', 'change'] }],
      costDistributionUri: [{ required: true, type: 'string', message: '请上传成本配置比例情况图', trigger: ['blur', 'change'] }],
      dramaDesp: [
        { required: true, type: 'string', message: '请输入短剧简介', trigger: ['blur', 'change'] },
        { min: dramaDespMin, max: dramaDespMax, type: 'string', message: `长度在 ${dramaDespMin} 到 ${dramaDespMax} 个字符`, trigger: ['blur', 'change'] },
      ],
      dramaRecommendation: [
        { required: true, type: 'string', message: '请输入推荐语', trigger: ['blur', 'change'] },
        { min: 0, type: 'string', message: `长度在 1 到 ${dramaRecommendationMax} 个字符`, trigger: ['blur', 'change'] },
      ],
      tagsList: [{ required: true, type: 'array', message: '请添加视频标签', trigger: ['blur', 'change'] }],
      seqNum: [
        { required: true, type: 'number', message: '请输入共多少集', trigger: ['blur', 'change'] },
        { min: 1, max: seqNumMax, type: 'number', message: `集数应在 1 到 ${seqNumMax} 之间`, trigger: ['blur', 'change'] },
      ],
      dramaYear: [{ required: true, type: 'string', message: '请选择拍摄年份', trigger: ['blur', 'change'] }],
      directorInfo: [{ required: true, type: 'object', message: '请添加导演', trigger: ['blur', 'change'] }],
      performerList: [{ required: true, type: 'array', message: '请添加演员', trigger: ['blur', 'change'] }],
      performerRoleList: [{ required: true, type: 'array', message: '请添加演员角色', trigger: ['blur', 'change'] }],
      producerInfo: [{ required: true, type: 'object', message: '请输入制片人', trigger: ['blur', 'change'] }],
    };

    if (formData.value.isFree !== 1) {
      _rules.freeEpisodeNum = [
        { required: true, type: 'number', message: '请输入免费集数', trigger: ['blur', 'change'] },
        { min: 0, max: formData.value.seqNum || 0, type: 'number', message: `免费集数应在 0 到 ${formData.value.seqNum || 0}`, trigger: ['blur', 'change'] },
      ];
      _rules.price = [
        { required: true, message: '请输入单集解锁价格', trigger: ['blur', 'change'] },
        { min: 0.1, max: allEpisodePriceMax, type: 'number', message: `单集解锁价格在 0.1 到 ${priceMax} 元`, trigger: ['blur', 'change'] },
      ];
      _rules.allEpisodePrice = [
        { required: true, message: '请输入全集解锁价格', trigger: ['blur', 'change'] },
        { min: 0.1, max: allEpisodePriceMax, type: 'number', message: `全集解锁价格在 0.1 到 ${allEpisodePriceMax} 元`, trigger: ['blur', 'change'] },
      ];
    }
    return _rules;
  });

  return {
    dramaTitleMax,
    productionCostMax,
    avgDurationMax,
    dramaDespMin,
    dramaDespMax,
    dramaRecommendationMax,
    allEpisodePriceMax,
    priceMax,
    seqNumMax,
    rules,
  };
}

/**
 * 裁剪框工具
 */
export function useCropperUtil() {
  const restImageCropperRef: $RefComp<'RestImageCropper'> = ref(null);

  /** 打开短剧海报裁剪框 */
  function openDramaCoverCropper(files: File[]): Promise<File[]> {
    return new Promise((resolve, reject) => {
      const file = files[0];
      if (file) {
        restImageCropperRef.value?.show({
          file,
          cropperOptions: { aspectRatio: 1080 / 1920, autoCropArea: 1 },
          croppedCanvasOptions: { minWidth: 720, maxWidth: 1080, maxHeight: 1920, minHeight: 1280 },
          fail: () => {
            reject(null);
          },
          success: (res: File) => {
            resolve([res] as File[]);
          },
        });
      } else {
        resolve(files);
      }
    });
  }
  /** 打开短剧海报缩略图裁剪框 */
  function openDramaCoverThumbCropper(files: File[]): Promise<File[]> {
    return new Promise((resolve, reject) => {
      const file = files[0];
      if (file) {
        restImageCropperRef.value?.show({
          file,
          cropperOptions: { aspectRatio: 1080 / 1920, autoCropArea: 1 },
          croppedCanvasOptions: { width: 432, height: 768 },
          fail: () => {
            reject(null);
          },
          success: (res: File) => {
            resolve([res] as File[]);
          },
        });
      } else {
        resolve(files);
      }
    });
  }
  return {
    restImageCropperRef,
    /** 打开短剧海报裁剪框 */
    openDramaCoverCropper,
    /** 打开短剧海报缩略图裁剪框 */
    openDramaCoverThumbCropper,
  };
}
